//
//  ZMDiscoverRecommendCircleCell.m
//  基本框架
//
//  Created by runone02 on 2018/7/3.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "ZMDiscoverRecommendCircleCell.h"

@interface ZMDiscoverRecommendCircleCell()
/**记录最后一个数量 */
@property (nonatomic, assign) NSInteger lastCount;
/**底部视图 */
@property (nonatomic, strong) UIView *bottonView;
/** 存储数据*/
@property (nonatomic, strong) NSArray *dataArray;
/** 最后一个按钮*/
@property (nonatomic, strong) ZMDiscoverRecommendCircleCellView *lastBtn;
/** */
@property (nonatomic, assign) CGFloat cellLineCount;
@property (nonatomic, assign) CGFloat cellLineSpace;
@property (nonatomic, assign) CGFloat cellWidth;
@property (nonatomic, assign) CGFloat cellHeight;
@property (nonatomic, assign) CGFloat leftMargin;
@end
@implementation ZMDiscoverRecommendCircleCell


- (UIView *)bottonView{
    if (!_bottonView) {
        _bottonView = [[UIView alloc]init];
        [self.contentView addSubview:_bottonView];
        [self.bottonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.bottom.mas_equalTo(0);
        }];
    }
    return _bottonView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [ZMColor whiteColor];
        self.cellLineCount = 3;
        self.cellLineSpace = 2;
        self.leftMargin = 10;
        self.cellWidth = (kScreenWidth - self.leftMargin * 2 - (self.cellLineCount - 1) * self.cellLineSpace)/3;
        self.cellHeight = self.cellWidth + 18 + 5 + 15 + 8 + 30 + 8;
    }
    for (UIView *view in self.subviews) {
        if([view isKindOfClass:[UIScrollView class]]) {
            ((UIScrollView *)view).delaysContentTouches = NO; // Remove touch delay for iOS 7
            break;
        }
    }
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundView.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
    return self;
}

- (void) setCircleCellUI:(NSArray *)array{
    self.dataArray = array;
    NSInteger count = array.count;
    
    //初始化视图
    for (NSInteger i = 1; i <= count; i++) {
        ZMDiscoverRecommendCircleCellView *cell = [self viewWithTag:i*10];
        if (!cell) {
            cell = [self setupCell:[self.dataArray safeObjectAtIndex:i-1] tag:i * 10];
            [cell mas_makeConstraints:^(MASConstraintMaker *make) {
                if (!self.lastBtn) {
                    make.left.mas_equalTo(self.bottonView.mas_left).offset(self.leftMargin);
                    make.top.mas_equalTo(self.bottonView.mas_top).with.offset(self.cellLineSpace);
                }else{
                    //左边
                    if (i % 3 == 1) {
                        make.left.mas_equalTo(self.bottonView.mas_left).offset(self.leftMargin);
                        NSUInteger line = ceil((i)/3.f) - 1;
                        make.top.mas_equalTo(line * (self.cellHeight  + self.cellLineSpace) + self.cellLineSpace);
                    }else if(i % 3 == 2){
                        make.left.mas_equalTo(self.cellWidth  + self.leftMargin + self.cellLineSpace);
                        NSUInteger line = ceil((i)/3.f) - 1;
                        make.top.mas_equalTo(line * (self.cellHeight  + self.cellLineSpace) + self.cellLineSpace);
                    }else if (i % 3 == 0){
                        make.right.mas_equalTo(self.bottonView.mas_right).offset(- self.leftMargin);
                        NSUInteger line = ceil((i)/3.f) - 1;
                        make.top.mas_equalTo(line * (self.cellHeight  + self.cellLineSpace) + self.cellLineSpace);
                    }
                }
            }];
            
        }else{
            //已有button 直接设置数据
            cell.model = [self.dataArray safeObjectAtIndex:i-1];
        }
        self.lastBtn = cell;
    }
    //删除
    for (NSInteger j = count; j < _lastCount; j++) {
        UIButton *btn = [self viewWithTag:j*10];
        [btn removeFromSuperview];
        btn = nil;
    }
    _lastCount = count;
}

- (ZMDiscoverRecommendCircleCellView *) setupCell:(ZMCircleModel *)model tag:(NSInteger)tag{
    ZMDiscoverRecommendCircleCellView *cell = [[ZMDiscoverRecommendCircleCellView alloc] initWithFrame:CGRectMake(0, 0, self.cellWidth, self.cellHeight)];
    cell.tag = tag;
    [self.bottonView addSubview:cell];
    [cell mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.cellWidth);
        make.height.mas_equalTo(self.cellHeight);
    }];
    //设置数据
    cell.model = model;
    return cell;
}

@end

@implementation ZMDiscoverRecommendCircleCellView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [ZMColor whiteColor];
        [self setUI];
    }
    return self;
}

- (void) setUI{
    _mainView = [[UIView alloc]init];
    _mainView.layer.masksToBounds = YES;
    _mainView.layer.borderColor = [ZMColor appLinghtGrayColor].CGColor;
    _mainView.layer.borderWidth = 0.5;
    _mainView.layer.cornerRadius = 3;
    [self addSubview:_mainView];
    [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    [_mainView.superview layoutIfNeeded];
    
    _thumbImageView = [[UIImageView alloc]init];
    _thumbImageView.layer.masksToBounds = YES;
    _thumbImageView.image = placeholderFailImage;
    _thumbImageView.layer.cornerRadius = (_mainView.width - 20 )* 0.5;
    [_mainView addSubview:_thumbImageView];
    [_thumbImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(self.mainView.width - 20);
    }];
    [_thumbImageView.superview layoutIfNeeded];
    
    _nameLabel = [[UILabel alloc]init];
    _nameLabel.font = TITLE_FONT(30);
    _nameLabel.textColor = [UIColor blackColor];
    _nameLabel.text = @"📱游戏";
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    [_mainView addSubview:_nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.thumbImageView.mas_bottom).with.offset(5);
        make.height.mas_equalTo(18);
    }];
    
    _countLabel = [[UILabel alloc]init];
    _countLabel.textColor = [ZMColor appSupportColor];
    _countLabel.font = TITLE_FONT(30);
    _countLabel.textAlignment = NSTextAlignmentCenter;
    _countLabel.text = @"人气是不存在";
    [_mainView addSubview:_countLabel];
    [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).with.offset(5);
        make.height.mas_equalTo(15);
    }];
    
    _intoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _intoButton.layer.cornerRadius = 3;
    _intoButton.backgroundColor = [ZMColor appMainColor];
    [_intoButton setTitle:@"进入" forState:UIControlStateNormal];
    [_intoButton addTarget:self action:@selector(intoButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_intoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_intoButton.titleLabel setFont:TITLE_FONT(30)];
    [_mainView addSubview:_intoButton];
    [_intoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(self.countLabel.mas_bottom).with.offset(8);
        make.height.mas_equalTo(30);
    }];
}

- (void) intoButtonClick{
    NSLog(@"进入专题");
    
}


- (void)setModel:(ZMCircleModel *)model {
    if (!model)    return;
    _model = model;
    NSString *rightCover = [NSString stringWithFormat:@"%@%@%@%@",HttpImageURLPre,model.circlePic,HttpImageURLSuffixSquare,model.imageSuffix];
    [self.thumbImageView setImageWithURL:[NSURL URLWithString:rightCover] placeholder:placeholderFailImage];
    self.nameLabel.text  = model.circleName;
    self.countLabel.text = model.allCount;
}
@end
